package com.heer.gymnasium.util;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.heer.gymnasium.R;
import com.heer.gymnasium.constant.Type;
import com.zhihuiyun.library.dialog.CommonDialog;

/**
 * Created by yan on 2018/1/18.
 * Dialog工具类
 */

public class DialogUtils {

    /**
     * 拍照等popupwindow
     *
     * @param context
     * @param layoutId
     * @return
     */
    public static PopupWindow getPopupWindow(final Activity context, int layoutId, DialogCallBack dialogCallBack) {
        View popupView = LayoutInflater.from(context).inflate(layoutId, null);

        final PopupWindow popupWindow = new PopupWindow(popupView,
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.WRAP_CONTENT);
        popupWindow.setAnimationStyle(R.anim.anim_popup);
        popupWindow.setBackgroundDrawable(new ColorDrawable(context.getResources().getColor(R.color.black_75_transparent)));
        // TODO: 2016/5/17 设置可以获取焦点
        popupWindow.setFocusable(true);
        // TODO: 2016/5/17 设置可以触摸弹出框以外的区域
        popupWindow.setOutsideTouchable(true);
        // TODO：更新popupwindow的状态
        popupWindow.update();
        dialogCallBack.callBack(popupView, popupWindow);
        return popupWindow;
    }


    private static CommonDialog myDialog;
    public static final int DIALOG_TYPE_SINGLE = 1;

    //启动自定义dialog
    public static void showDialog(Activity context, String title, String content, String leftText, String rightText, int type,
                                  View.OnClickListener onClickListener) {
        if (context != null) {
            myDialog = new CommonDialog(context, R.style.dialog_common, title, content, leftText, rightText, type, onClickListener);
            myDialog.setCanceledOnTouchOutside(false);
            myDialog.show();
        }
    }


    //启动自定义dialog
    public static void showDialog(Activity context, String title, String content, String leftText, String rightText,
                                  View.OnClickListener onClickListener) {
        showDialog(context, title, content, leftText, rightText, 0, onClickListener);
    }

    /**
     * 自动续订对话框
     *
     * @param activity
     * @param callBackForReservation
     * @return
     */
    public static Dialog dialogForReservation(Activity activity, final CallBackForReservation callBackForReservation) {
        Dialog dialog = new Dialog(activity) {
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.dialog_auto_reservation);
                final EditText editText = (EditText) this.findViewById(R.id.dialog_auto_reservation_num_et);
                this.findViewById(R.id.dialog_auto_reservation_confirm_tv).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        callBackForReservation.callBack(view, editText.getText().toString());
                        dismiss();

                    }
                });
                this.findViewById(R.id.dialog_auto_reservation_cancle_iv).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        dismiss();
                    }
                });
            }
        };
        dialog.show();
        return dialog;
    }

    public interface CallBackForReservation {
        void callBack(View view, String num);
    }

    //销毁dialog
    public static void dismissDialog() {
        if (myDialog.isShowing())
            myDialog.dismiss();
    }

    public interface DialogCallBack {
        void callBack(View view, PopupWindow popupWindow);
    }
}
